// components/Video/index.js
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        videoUrl:{
            type:"String",
            value:""
            
        },
        videoImgUrl:{
             type:"String",
             value:""
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
    
    //  时间
        currentTime:'00:00',
        

        duration:'00:00',

        progerssValue:0,


        allTimes:0,
        
    //  视频状态
        videoPlay:false, 


        isPlay:false,


        initPlay:false,


        controls:false,
    //  设置屏幕
        orientation:'portrait'
    
    


        

    },
    pageLifetimes:{
      hide(){
          console.log('hide')
          let isPlay=this.data.isPlay

          if(isPlay){
          
           let timer= setTimeout(()=>{

             clearTimeout(timer)

             this.setData({
                 isPlay
             })
               
           },500)

          }
      }
    },

    attached: function () {
        this.data.videoContent=wx.createVideoContext('myVideo', this)
    },
   
  
  

    /**
     * 组件的方法列表
     */
    methods: {

        pause(){
           if(this.data.progerssValue==0){
                this.data.videoContent.seek(0)
            }
            this.setData({
             isPlay:false
            })
        },
        play(){
          
            this.setData({
                isPlay:true
               })
        
          
        },

        controlstoggle(){
          console.log('controlstoggle')
        },

        handleScreen(){
          
            this.data.videoContent.requestFullScreen()
        },

        keepVisible(){
            this.setData({
                visible:true
            },()=>{
                clearTimeout(this.data.visible_timer)
               this.data.visible_timer=setTimeout(()=>{
                  this.setData({
                    visible:false
                  })
               },5000)
            })
        },
      
        clickVideo(e){
            
        
        if(!this.data.initPlay){
            this.data.videoContent.play()
  
                this.setData({
    
                    initPlay:true,
                    isPlay:true
                })

            this.keepVisible()
    
        }
       else if(this.data.initPlay&&e.target.dataset.type=='video'){
         
        let visible=this.data.visible

        if(!visible){
        
        
            this.setData({
                visible:true
            },()=>{
                clearTimeout(this.data.visible_timer)
               this.data.visible_timer=setTimeout(()=>{
                  this.setData({
                    visible:false
                  })
               },5000)
            })
        }else{
            this.setData({
                visible:false
            })

        }

        
       }

        

        },
        clickPlayBtn(e){
            console.log('clickPlayBtn')
            if(this.data.isPlay){
                this.data.videoContent.pause()
            }else{
             this.data.videoContent.play()
            }

          
             this.setData({
                isPlay:!this.data.isPlay
              })
          
        },
        
        //拖动进度条修改slide值
        sliderchanging(e){
           

            if(this.data.isPlay){
                this.setData({
                  isPlay:false
                })
                this.data.videoContent.pause()
            }
      
       
            clearTimeout(this.data.timer)
       

         this.data.timer=setTimeout(() => {

            this.keepVisible()

            let value=e.detail.value
  
            let allTimes=this.data.allTimes


             let currentTime =allTimes*(value/100)

             let initTime=currentTime
             
             this.data.videoContent.seek(currentTime)

        
             this.setData({
                 initTime,
                 currentTime:this.getStrTime(currentTime)
                 
             })
             
           
             
         }, 100);
  
  
        
        },
        // 修改视频进度值
        sliderchange(e){
         

            this.keepVisible()
         
        
            if(this.data.isPlay){
                this.data.videoContent.pause()
                this.setData({
                    isPlay:false
                })
              
            }

          let value=e.detail.value


          let allTimes=this.data.allTimes


          let currentTime =allTimes*(value/100)

          console.log('currentTime',currentTime)
  
          this.data.videoContent.seek(currentTime)
       

            this.setData({
                isPlay:true,
            },()=>{
                this.data.videoContent.play()
            })
             
       
         
        
        },
        //加载信息
        handleLoad(e){

          

          let {duration} = e.detail


          let allTimes=duration

          duration=this.getStrTime(duration)
          
          this.setData({
            duration,
            allTimes
          })
        },
        fullscreenchange(e){

            
                this.setData({
                    controls: !!e.detail.fullScreen
                })
              
          
        },
       //打开全屏或者关闭全屏
        handleFullScreen(e){
    

      if(wx.setPageOrientation){

        let orientation=this.data.orientation=='portrait'?'landscape':'portrait'
        let  that=this

        
        wx.setPageOrientation({
            orientation,
            success:function(){
                that.setData({
                orientation  
                })
                wx.setNavigationBarRightButton({
                    hide: orientation=='landscape'
                });
               
            }
        })


      }else{
         this.data.videoContent.requestFullScreen()
      }
    
    
          
        },
       //监听视频进度
         getProgress(e){
        
          if(this.data.isPlay){

            let {currentTime,duration}=e.detail
            
            
            let progerssValue=(currentTime/duration)*100


         
            this.setData({
              currentTime:this.getStrTime(currentTime),
              progerssValue
            })

          }

          
          
         },
      //播放视频结束
         playEnd(e){
            
          
            this.setData({
              currentTime:'00:00',
              progerssValue:0,
              initTime:0,
              
            })
            

         },

     

        getStrTime(time=0){

            
            let min = Math.floor(time/60)

            min=min<10?'0'+min:min

            let s= Math.floor(time%60) 

            s=s<10?'0'+s:s


            return min+":"+s

        }
        
        


    },
  
   
})
